using System.Collections.Generic;
using System.IO;

public class LuaSearchPathFinder
{
    /// <summary>
    /// 判断这个lua是否存在 ;
    /// lua脚本路径获取完整文件路径
    /// </summary>
    /// <param name="lua"></param>
    /// <param name="path"></param>
    /// <returns></returns>
    public static bool IsFindLuaModule(string lua, out string path)
    {
        path = "";
        if (string.IsNullOrEmpty(lua))
            return false;
        foreach (var p in LuaBehaviour.SearchPaths)
        {
            var full = Path.Combine(p, lua);
            if (!full.EndsWith(".lua"))
                full += ".lua";
            if(File.Exists(full) || File.Exists(Path.GetFullPath(full)))
            {
                path = full;
                return true;
            }
        }
        path = "";
        return false;
    }
    /// <summary>
    /// 判断这个文件是否是lua文件 ; 
    /// lua完整路径获取lua脚本路径
    /// </summary>
    /// <param name="path"></param>
    /// <param name="lua"></param>
    /// <returns></returns>
    public static bool IsFindLuaPath(string path, out string lua)
    {
        lua = "";
        if (string.IsNullOrEmpty(path))
            return false;
        FileInfo fip = new FileInfo(path);
        if (!fip.Exists)
            return false;
        foreach (var p in LuaBehaviour.SearchPaths)
        {
            DirectoryInfo di = new DirectoryInfo(p);
            if (di.Exists)
            {
                if (fip.Directory.FullName.Contains(di.FullName))
                {
                    lua = Path.GetRelativePath(di.FullName,fip.FullName);
                    return true;
                }
            }
        }   
        return false;
    }
}